import 'dart:convert';

import 'package:fluro/fluro.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:hxdj/common/base_page/base_page.dart';
import 'package:hxdj/common/dio/dio_http.dart';
import 'package:hxdj/common/my_common.dart';
import 'package:hxdj/common/my_constant.dart';
import 'package:hxdj/common/my_global.dart';
import 'package:hxdj/common/routers/navigator.dart';
import 'package:hxdj/login/user_model.dart';
import 'package:hxdj/splash.dart';

class LoginViewModel extends BaseViewModel {
  //图形验证码
  String captchaBase64 = "";

  //是否展示图形验证码
  bool isShowGraphicVerificationCode = false;

  //是否点击了同意协议&政策
  bool isAgreeProtocol = false;

  //是否获取验证码
  bool isGetSmsCode = false;

  //未读消息的数量
  int totalUnreadCount = 0;

  setCaptcha(String prams) {
    captchaBase64 = prams;
    notifyListeners();
  }

  setIsGetSmsCode(bool isGet) {
    isGetSmsCode = isGet;
    notifyListeners();
  }

  //是否同意隐私政策
  setAgreeProtocol() {
    isAgreeProtocol = !isAgreeProtocol;
    notifyListeners();
  }

  // 登录账号
  Future<void> login(String mobile, String code) async {
    // 去请求服务器数据.
    Map<String, dynamic> resultMap = await DioHttpManager().request(MyApi.loginOn,
        method: DioHttpManager.post, data: {"mobile": mobile, "code": code, "login_type": "1"}, isLoading: true);

    if (resultMap['code'] == 0) {
      //把用户信息, 保存到本地
      CommonPlugin.sharedPreferences!.setString(MyConstant.userInfo, json.encode(resultMap["data"]));
      UserModel userModel = UserModel.fromMap(resultMap["data"]);

      CommonPlugin.sharedPreferences!.setString(MyConstant.token, userModel.token ?? "");
      CommonPlugin.sharedPreferences!.setString(MyConstant.userAvatar, userModel.picUrl ?? "");
      CommonPlugin.sharedPreferences!.setString(MyConstant.userNmae, userModel.nickName ?? "");
      CommonPlugin.sharedPreferences!.setString(MyConstant.memberId, userModel.memberId.toString());

      debugPrint("userModel = ${userModel.imPass}");
      //给全局的Provider 赋值
      MyGlobalViewModel globalViewModel = MyGlobalViewModel.getGlobalViewModel(context);
      globalViewModel.userModel = userModel;

      CommonPlugin.sharedPreferences!.setBool(MyConstant.isLogin, true);

      globalViewModel.refresh();
      //跳转到主页面
      NavigatorUtils.pushParams(context, MyMainPage.routeName,
          clearStack: true, replace: true, transition: TransitionType.fadeIn);
      EasyLoading.showToast("登录成功");
    } else {
      EasyLoading.showToast(resultMap["msg"]);
    }
  }

  //去请求服务器数据.获取验证码
  updateSmsVerificationCode(String mobile, {String? captcha}) async {
    debugPrint("updateSmsVerificationCode =");

    Map<String, dynamic> parmaMap = {"mobile": mobile};
    if (isShowGraphicVerificationCode) {
      parmaMap["captcha"] = captcha ?? "";
    }

    final parmaString = "?mobile=$mobile${isShowGraphicVerificationCode ? '&captcha=$captcha' : ''}";

    Map<String, dynamic> resultMap = await DioHttpManager()
        .request(MyApi.getsms + parmaString, method: DioHttpManager.get, data: parmaMap, isLoading: true);

    // 去请求服务器数据.
    await Future.delayed(const Duration(seconds: 1));
    if (resultMap['code'] == 0) {
      EasyLoading.showToast("发送验证码成功");

      //开始倒计时
      setIsGetSmsCode(true);
    } else {
      isShowGraphicVerificationCode = true;
      //去获取图形验证码
      updateGraphicVerificationCode();

      // EasyLoading.showToast("发送验证码失败, 请输入图形验证码");
      EasyLoading.showToast(resultMap["msg"]);
    }
  }

  //获取图形验证码
  updateGraphicVerificationCode() async {
    debugPrint("updateGraphicVerificationCode =");

    Map<String, dynamic> resultMap =
        await DioHttpManager().request(MyApi.getsmsCaptcha, method: DioHttpManager.post, isLoading: true);

    // 去请求服务器数据.
    if (resultMap['code'] == 0) {
      setCaptcha(resultMap['data']["result"]);
    }
  }
}
